<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24" >
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题" ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="星期几" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dayOfWeek">
              <a-input  v-model="model.dayOfWeek" placeholder="请输入星期几"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input readOnly v-model="model.name" placeholder="请输入姓名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="data">
              <a-input readOnly v-model="model.data" placeholder="请选择日期"></a-input>
              <!-- <j-date readOnly placeholder="请选择日期" v-model="model.data" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" /> -->
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="每日记录明细表" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="chDailyRecordDetailsTable.loading"
          :columns="chDailyRecordDetailsTable.columns"
          :dataSource="chDailyRecordDetailsTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"
        />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>
import { getAction } from '@/api/manage'
import { FormTypes, getRefPromise, VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ChDailyRecordForm',
  mixins: [JEditableTableModelMixin],
  components: {},
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      model: {
        name: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')).realname : '',
        dayOfWeek: '',
        data: '',
      },
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 1,
      validatorRules: {
        title: [{ required: true, message: '请输入标题!' }],
        dayOfWeek: [{ required: true, message: '请输入星期几!' }],
        name: [{ required: true, message: '请输入姓名!' }],
        data: [{ required: true, message: '请输入日期!' }],
      },
      refKeys: ['chDailyRecordDetails'],
      tableKeys: ['chDailyRecordDetails'],
      activeKey: 'chDailyRecordDetails',
      // 每日记录明细表
      chDailyRecordDetailsTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '待办事项',
            key: 'toDoList',
            type: FormTypes.input,
            width: '600px',
            placeholder: '请输入${title}',
            defaultValue: '',
            validateRules: [{ required: true, message: '${title}不能为空' }],
          },
          {
            title: '完成比例',
            key: 'completeRatio',
            type: FormTypes.inputNumber,
            width: '200px',
            placeholder: '请输入${title}',
            defaultValue: 100,
            validateRules: [{ required: true, message: '${title}不能为空' }],
          },
          {
            title: '状态',
            key: 'status',
            type: FormTypes.select,
            width: '200px',
            placeholder: '请输入${title}',
            options:[
              {
                text:'未开始',
                value:'未开始'
              },
              {
                text:'进行中',
                value:'进行中'
              },
              {
                text:'已完成',
                value:'已完成'
              }

            ],
            defaultValue: '已完成',
            validateRules: [{ required: true, message: '${title}不能为空' }],
          },
          // {
          //   title: '每日记录表id',
          //   key: 'chDailyRecordId',
          //   type: FormTypes.input,
          //   width: '200px',
          //   placeholder: '请输入${title}',
          //   defaultValue: '',
          // },
        ],
      },
      url: {
        add: '/ch/chDailyRecord/add',
        edit: '/ch/chDailyRecord/edit',
        queryById: '/ch/chDailyRecord/queryById',
        chDailyRecordDetails: {
          list: '/ch/chDailyRecord/queryChDailyRecordDetailsByMainId',
        },
      },
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    // 计算时间
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() < 10 ? '0' + (date.getMonth() +1) : date.getMonth()+1
    var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    this.model.data = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + second
    console.log('date',date);
    console.log('date.getMonth',date.getMonth());
    console.log('month',month);
    // 计算星期
    var day = date.getDay()
    var weeks = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六')
    var week = weeks[day]
    this.model.dayOfWeek = week
  },
  methods: {
    addBefore() {
      this.chDailyRecordDetailsTable.dataSource = []
    },
    getAllTable() {
      let values = this.tableKeys.map((key) => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter() {
      this.$nextTick(() => {})
      // 加载子表数据
      if (this.model.id) {
        let params = { id: this.model.id }
        this.requestSubTableData(this.url.chDailyRecordDetails.list, params, this.chDailyRecordDetailsTable)
      }
    },
    //校验所有一对一子表表单
    validateSubForm(allValues) {
      return new Promise((resolve, reject) => {
        Promise.all([])
          .then(() => {
            resolve(allValues)
          })
          .catch((e) => {
            if (e.error === VALIDATE_NO_PASSED) {
              // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
              this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
            } else {
              console.error(e)
            }
          })
      })
    },
    /** 整理成formData */
    classifyIntoFormData(allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        chDailyRecordDetailsList: allValues.tablesValue[0].values,
      }
    },
    validateError(msg) {
      this.$message.error(msg)
    },
  },
}
</script>

<style scoped>
</style>